package com.ruoyi.web.controller.hotel;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.hotel.domain.LyhmsOrder;
import com.ruoyi.hotel.service.ILyhmsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Controller
 * @author ruoyi
 * @date 2022-02-28
 */
@RestController
@RequestMapping("/system/order")
public class LyhmsOrderController extends BaseController {
    @Autowired
    private ILyhmsOrderService lyhmsOrderService;

    /**
     * 查询列表
     */
    @GetMapping("/list")
    public TableDataInfo list(LyhmsOrder lyhmsOrder) {
        startPage();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String userName = user.getNickName();
        List<SysRole> roles = user.getRoles();
        boolean flag = false;
        for (SysRole role : roles) {
            if(100 == role.getRoleId() || 2 ==role.getRoleId()){
                flag = true;
            }
        }
        if(flag){
            lyhmsOrder.setOrderOwnerName(userName);
        }

        List<LyhmsOrder> list = lyhmsOrderService.selectLyhmsOrderList(lyhmsOrder);
        return getDataTable(list);
    }

    /**
     * 导出列表
     */
    @Log(title = "", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LyhmsOrder lyhmsOrder) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String userName = user.getNickName();
        List<SysRole> roles = user.getRoles();
        boolean flag = false;
        for (SysRole role : roles) {
            if(100 == role.getRoleId() || 2 ==role.getRoleId()){
                flag = true;
            }
        }
        if(flag){
            lyhmsOrder.setOrderOwnerName(userName);
        }
        List<LyhmsOrder> list = lyhmsOrderService.selectLyhmsOrderList(lyhmsOrder);
        ExcelUtil<LyhmsOrder> util = new ExcelUtil<LyhmsOrder>(LyhmsOrder.class);
        util.exportExcel(response, list, "数据");
    }

    /**
     * 获取详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(lyhmsOrderService.selectLyhmsOrderById(id));
    }

    /**
     * 新增
     */
    @Log(title = "", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LyhmsOrder lyhmsOrder) {
        return toAjax(lyhmsOrderService.insertLyhmsOrder(lyhmsOrder));
    }

    /**
     * 修改
     */
    @Log(title = "", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LyhmsOrder lyhmsOrder) {
        return toAjax(lyhmsOrderService.updateLyhmsOrder(lyhmsOrder));
    }

    /**
     * 删除
     */
    @Log(title = "", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(lyhmsOrderService.deleteLyhmsOrderByIds(ids));
    }


    @GetMapping("quxiao/{id}")
    public AjaxResult quxiao(Long id) {
        return toAjax(lyhmsOrderService.quxiao(id));
    }

    @PutMapping("tuifang")
    public AjaxResult tuifang(@RequestBody LyhmsOrder lyhmsOrder) {
        return toAjax(lyhmsOrderService.tuifang(lyhmsOrder));
    }

    @PutMapping("ruzhu")
    public AjaxResult ruzhu(@RequestBody LyhmsOrder lyhmsOrder) {
        return toAjax(lyhmsOrderService.ruzhu(lyhmsOrder));
    }
}
